package cn.ainit.byf.wordbook;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Repository("wordDao")
public class WordDaoImpl implements WordDao {
	
	private SessionFactory sessionFactory;
	
	@Resource
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public List<ByfBascodeitem> getAllItem(String uid) {
		String hql = "from ByfBascodeitem b where b.codeSetId = ? ";
		Query query =  sessionFactory.getCurrentSession().createQuery(hql);;
		query.setString(0, uid);
		return query.list();
	}
	
	@Override
	public List<ByfBascodeset> getWord() {
		String hql = "from ByfBascodeset w";
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		return  query.list();
	}

	@Override
	public List<ByfBascodesetclass> getWords(String id) {
		String hql = "from ByfBascodeset where codesetid=?";
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, id);
		return query.list();
	}
	
	@Override
	public void addWord(ByfBascodeitem byfBascodeitem) {
		sessionFactory.getCurrentSession().save(byfBascodeitem);
	}
	
	@Override
	public boolean delWord(String id){
		String hql = "delete ByfBascodeitem b where b.codeId=?";
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, id);
		return (query.executeUpdate()>0);
	}
	
	
	@Override
	public List<ByfBascodeitem> getItem(String item) {
		String hql = "from ByfBascodeitem b where b.codeId=?";
		Query query =  sessionFactory.getCurrentSession().createQuery(hql);;
		query.setString(0, item);
		return query.list();
	}

	@Override
	public boolean updateWord(ByfBascodeitem item) {
		String hql = "update ByfBascodeitem b set b.codeSetId = ?,b.name=?,b.sortNumber=?,b.helpCode=?,b.isSystem=? where b.codeId = ?";
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, item.getCodeSetId());
		query.setString(1, item.getName());
		query.setInteger(2, item.getSortNumber());
		query.setString(3, item.getHelpCode());
		query.setString(4, item.getIsSystem());
		query.setString(5, item.getCodeId());
		return (query.executeUpdate() > 0);
	}
}
